package com.runachina.appversion;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.runachina.R;
import com.runachina.common.BaseActivity;
import com.runachina.util.MyUtils;

/**
 * Created by lyl on 2017/6/9.
 * <p>
 * 检查版本更新
 */

public class QueryAppVersion extends BaseActivity {
    /**
     * 版本更新
     */
    public static final String VERSION_UPDATE_TITLE = "版本更新";
    public static final String VERSION_UPDATE_MESSAGE = "检测到新版本，是否立即更新";
    public static final String VERSION_NO_UPDATE_MESSAGE = "已是最新版本";

    private Context context;
    private View rootView;
    private int currentVersionCode;//当前安装版本号
    private String apkUrl;
    public AppVersion appVersion;

    public QueryAppVersion(View rootView, Context context) {
        this.rootView = rootView;
        this.context = context;
        appVersion = new AppVersion();
    }

    public void queryData() {

        new QueryNewVersion().query(new QueryNewVersion.ResponseListener() {
            @Override
            public void onResponse(AppVersion version) {
                appVersion.setVersionCode(version.getVersionCode());
                appVersion.setVersionName(version.getVersionName());
                appVersion.setApkUrl(version.getApkUrl());
                appVersion.setApkSize(version.getApkSize());
                appVersion.setUpdateMsg(version.getUpdateMsg());
                //检测版本信息
                checkVersionInfo(appVersion);
            }

            @Override
            public void onError(String error) {
//                tvNewInfo.setVisibility(View.GONE);
//                showToast("查询失败");
            }
        });
    }

    /**
     * 对比版本信息，是否需要更新
     */
    private void checkVersionInfo(AppVersion appVersion) {
        int netVersionCode = appVersion.getVersionCode();
        int appVersionCode = MyUtils.getAppVersionCode(context);
        if (netVersionCode > appVersionCode) {
            showPop(rootView, VERSION_UPDATE_TITLE, VERSION_UPDATE_MESSAGE);
        } else {
//            showPop(rootView, VERSION_UPDATE_TITLE, VERSION_UPDATE_MESSAGE);
//            Toast.makeText(context, "appversion:" + appVersion.getApkUrl(), Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 弹出框
     *
     * @param title   标题
     * @param message 信息
     */
    public void showPop(View rootView, String title, String message) {
        View popView = LayoutInflater.from(context).inflate(R.layout.pop_window, null);
        TextView btn_cancel = (TextView) popView.findViewById(R.id.btn_cancel);
        TextView btn_confirm = (TextView) popView.findViewById(R.id.btn_confirm);
        TextView tv_pop_title = (TextView) popView.findViewById(R.id.tv_pop_title);
        TextView tv_pop_message = (TextView) popView.findViewById(R.id.tv_pop_message);

        RelativeLayout.LayoutParams layoutParams;
        layoutParams = (RelativeLayout.LayoutParams) tv_pop_message
                .getLayoutParams();
        layoutParams.setMargins(0, 80, 0, 0);//4个参数按顺序分别是左上右下
        tv_pop_message.setLayoutParams(layoutParams);
        tv_pop_message.setGravity(Gravity.CENTER);
        tv_pop_title.setText(title);
        tv_pop_message.setText(message);
        final PopupWindow pop = new PopupWindow(popView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackAlpha(0.7f);
        //设置为false 点击外部不会消失
        pop.setFocusable(false);
        pop.setBackgroundDrawable(null);
        pop.setOnDismissListener(new PopdismisListener());
        pop.setOutsideTouchable(false);
        pop.showAtLocation(rootView, Gravity.CENTER, 0, 0);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pop!=null){
                    isQueryAppversion=false;
                    pop.dismiss();
                }
            }
        });
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //版本更新
                DownLoadManager downLoadManager = new DownLoadManager(context, appVersion.getApkUrl());
                downLoadManager.initPermission();
                pop.dismiss();
            }
        });
    }

    class PopdismisListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            setBackAlpha(1f);
        }
    }

    /**
     * 设置弹出框弹出时，外部变暗
     *
     * @param alpha
     */
    public void setBackAlpha(float alpha) {
        Activity activity = (Activity) context;
        WindowManager.LayoutParams lpParams = activity.getWindow().getAttributes();
        lpParams.alpha = alpha;
        activity.getWindow().setAttributes(lpParams);
    }
}
